package com.xwj.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xwj.back.mapper.MenuMapper;
import com.xwj.back.pojo.Menu;
import com.xwj.back.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> getMenu(Integer roleId) {
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.orderByAsc("id");
        menuQueryWrapper.inSql("id","select menu_id from role_menu where role_id = "+roleId);
        List<Menu> menus = menuMapper.selectList(menuQueryWrapper);
        //找出pid为null的一级菜单
        List<Menu> parentMenus = menus.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        //找出一级菜单的子菜单
        for (Menu parentMenu : parentMenus) {
            //每个一级菜单的子菜单列表
            parentMenu.setChildren(menus.stream()
                    .filter(menu -> parentMenu.getId().equals(menu.getPid())).collect(Collectors.toList()));
        }
        return parentMenus;
    }
}
